<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

/**
 * 导航管理
 * Class Nav
 * @package app\admin\controller
 */
class Hsnav extends AdminBase
{

    protected function _initialize()
    {
        parent::_initialize();
        $navcat_list = [
            1 =>'网站导航',
        ];
        $this->assign('navcat_list',$navcat_list);

        $nav_cid = input('nav_cid',1);
        $where['nav_cid'] = $nav_cid;
        $nav_list               = db('nav')->order(['sort' => 'ASC', 'id' => 'ASC'])->where($where)->select();
        $nav_level_list         = array2level($nav_list);
        $this->assign('nav_level_list',$nav_level_list);
        $this->assign('nav_cid',$nav_cid);
    }

    /**
     * 导航管理
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    /**
     * 添加导航
     * @param string $pid
     * @return mixed
     */
    public function add($pid = '',$nav_cid = '')
    {
        return $this->fetch('add', ['pid' => $pid,'nav_cid' => $nav_cid]);
    }

    /**
     * 保存导航
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->except('content','post');//排除content，接收post值
            $data['content'] = $_POST['content'];
            $data['alias'] = $_POST['alias'];
            $data['id'] = (db('nav')->max('id'))+1;
            if (db('nav')->insert($data)) {
                $this->success('保存成功','hsnav/index');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑导航
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        return $this->fetch('edit', ['nav' => db('nav')->find($id)]);
    }

    /**
     * 更新导航
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data = $this->request->except('content','post');//排除content，接收post值
            $data['content'] = $_POST['content'];
            $data['alias'] = $_POST['alias'];
            //halt($data);
            if (db('nav')->update($data) !== false) {
                $this->success('更新成功','hsnav/index');
            } else {    
                $this->error('更新失败');
            }
        }
    }

    /**
     * 更新菜单状态
     * @param $id
     */
    public function setStatus($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('nav')->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    
    /**
     * 删除导航
     * @param $id
     */
    public function delete($id)
    {
        $sub_menu = db('nav')->where(['pid' => $id])->find();
        if (!empty($sub_menu)) {
            $this->error('此菜单下存在子菜单，不可删除');
        }
        if (db('nav')->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}